package ui.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.CommomDialog;
import adapter.CourseraInfoAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.AfterSchoolCourseraBean;
import bean.ScheduleTableBean;
import student.andlei.com.student.R;
import utils.GlideUtils;
import utils.GsonUtils;
import utils.StringUtils;
import view.CircleImageView;

public class CourseraInfoActivity extends BaseNetActivity{
    private CircleImageView circleImageView;
    private TextView tv_teacher,
                    tv_class,
                    tv_time,
                    tv_classtime,
                    tv_title;
    private RecyclerView recyclerView;
    private CourseraInfoAdapter adapter;
    private List<ScheduleTableBean.Rooms> list = new ArrayList<>();
    private int schedule_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("课程信息");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            AfterSchoolCourseraBean bean = (AfterSchoolCourseraBean) GsonUtils.getInstance(context).parseComplexJArrayByCommon(baseBean,AfterSchoolCourseraBean.class);
            list = bean.rooms;
            GlideUtils.loadImageViewHeadphotoLoding(context,""+bean.schedule_image,circleImageView);
            if(StringUtils.isNull(bean.teacher_name)){bean.teacher_name = null;}
            tv_teacher.setText("主讲 ："+bean.teacher_name);
            tv_class.setText(bean.schedule_name);
            tv_time.setText("时间 ："+bean.start_date+"---"+bean.end_date);
            tv_classtime.setText("课时 ："+bean.room_number);
            adapter.setNewData(list);
        }else {
            toast(""+baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_courserainfo;
    }

    @Override
    protected void findViews() {
        circleImageView = getView(R.id.cimg_photo);
        tv_teacher = getView(R.id.tv_teacher);
        tv_class = getView(R.id.tv_class);
        tv_time = getView(R.id.tv_time);
        tv_classtime = getView(R.id.tv_classtime);
        tv_title = getView(R.id.tv_title);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter  = new CourseraInfoAdapter(R.layout.item_courserainfo,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    if(view.getId() == R.id.bt_){
                        Button mbutton = (Button) view;
                        if (mbutton.getText().toString().trim().equals("查看录播")) {
                            switch (list.get(position).classroom_status) {
                                case 3:
                                    Bundle bundle = new Bundle();
                                    bundle.putString("media_type", "videoondemand");
                                    bundle.putString("decode_type", "software");
                                    // http://ohjdda8lm.bkt.clouddn.com/course/sample1.mp4  "http://jdvodqbt9dzw0.vod.126.net/jdvodqbt9dzw0/f50c3a97-98a7-4467-aefc-82089485e88c.flv"
                                    // http://jdvodqbt9dzw0.vod.126.net/jdvodqbt9dzw0/18c14535-b1e4-4a21-84d2-e560774df0ec.flv
                                    bundle.putString("videoPath", list.get(position).video);
                                    jumpTo(VodActivity.class, bundle);
                                    break;
                                case 4:
                                    new CommomDialog(CourseraInfoActivity.this, R.style.dialog, "录播正在上传中，请稍等！", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            if (confirm) {
                                                dialog.dismiss();
                                            }
                                        }
                                    }).setTitle("提示").hideNegativeButton().settPositiveButtonColor(R.color.homepage_titlefont).show();
                                    break;
                            }
                        } else if (mbutton.getText().toString().trim().equals("进入课堂")) {
                            switch (list.get(position).classroom_status) {
                                case 1:
                                    new CommomDialog(CourseraInfoActivity.this, R.style.dialog, "课程尚未开始，请稍等！", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            if (confirm) {
                                                dialog.dismiss();
                                            }
                                        }
                                    }).setTitle("提示").hideNegativeButton().settPositiveButtonColor(R.color.homepage_titlefont).show();
                                    break;
                                case 2:
                                    Map<String, String> map = new HashMap<>();
                                    map.put("room_id", "" + list.get(position).room_id);
                                    post("Schedule/joinRoom", map, BaseBean.class, true);
                                    break;
                            }
                        }
                    }
            }
        });
    }

    @Override
    protected void formatData() {
//        for(int i=0;i<3;i++){
//           CourseraInfoBean bean = new CourseraInfoBean();
//           list.add(bean);
//
//        }
        loadData();
    }
    private void loadData(){
        Map<String,Object> map = new HashMap<>();
        map.put("schedule_id",""+schedule_id);
        post("schedule/scheduleDetail",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        schedule_id = bundle.getInt("schedule_id");
    }

    @Override
    public void onClick(View view) {

    }
}
